package model;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

import threads.AnalyPacket;

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.packet.Packet;

public class App {
	private static NetworkInterface[] devices = JpcapCaptor.getDeviceList();
	private static JComboBox<String> jBox = new JComboBox<String>();
	private static PackTableModel table = null;
	private static JpcapCaptor captor = null;
	private static AnalyPacket nowthread = null;
	private static String filterString = "";
	private static int filterCount = Integer.MAX_VALUE;
	// 全局变量
	public static Map<Short, String> ProtocalMap = new HashMap<Short, String>();
	public static List<Packet> packetList = null;
	public static long packetTime = 0;

	static {
		ProtocalMap.put((short) -1, "arp");
		ProtocalMap.put((short) 1, "icmp");
		ProtocalMap.put((short) 2, "igmp");
		ProtocalMap.put((short) 4, "ip");
		ProtocalMap.put((short) 6, "tcp");
		ProtocalMap.put((short) 17, "udp");
		ProtocalMap.put((short) 41, "ipv6");
		ProtocalMap.put((short) 121, "smp");
	}

	// 开始监听
	public static void startCapture(String filter) throws IOException {
		if (table == null || jBox == null) {
			System.out.println("Table Model or JComboBox is not ready !!");
			return;
		}
		System.out.println("-----------");
		System.out.println(filterString);
		System.out.println("-----------");
		int index = jBox.getSelectedIndex();
		captor = JpcapCaptor.openDevice(devices[index], 65535, true, 100);
		captor.setFilter(filterString, true);
		// 重置变量
		table.clear();
		packetList = null;
		packetTime = 0;
		AnalyPacket ap = new AnalyPacket(captor, table);
		ap.setMaxcount(filterCount);
		ap.start();
		nowthread = ap;
	}

	// 停止监听(所有数据重置)
	public static void closeCapture() {
		if (nowthread != null)
			nowthread.end();
		filterString = "";
		filterCount = Integer.MAX_VALUE;
	}

	// 设置过滤器们
	// 正则
	public static void setFilterReg(String reg) {
		if (!reg.equals(""))
			filterString = reg;
	}

	// 协议
	public static void setFilterPro(String reg) {
		if (reg.equals(""))
			return;
		String[] arr = reg.split(" ");
		String tmp = "";
		for (int i = 0; i < arr.length; i++) {
			if (ProtocalMap.containsValue(arr[i].toLowerCase())) {
				tmp += arr[i] + "||";
			}
		}
		if (tmp.length() > 2) {
			tmp = tmp.substring(0, tmp.lastIndexOf("||"));
		}
		if (filterString.equals(""))
			filterString += "(" + tmp + ")";
		else
			filterString += "&&(" + tmp + ")";
	}

	// 目的地址
	public static void setFilterDes(String reg) {
		if (reg.equals(""))
			return;
		if (filterString.equals(""))
			filterString += "src " + reg;
		else
			filterString += "&&src " + reg;
	}

	// 源地址
	public static void setFilterRes(String reg) {
		if (reg.equals(""))
			return;
		if (filterString.equals(""))
			filterString += "dst " + reg;
		else
			filterString += "&&dst " + reg;
	}

	// 抓包数
	public static void setFilterNum(String reg) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(reg);
		if (!isNum.matches()) {
			filterCount = Integer.valueOf(reg);
		}
	}

	public static void setFilterKey(String reg) {
		// 关键词
	}

	// 初始化数据列表
	public static void setTable(PackTableModel table) {
		App.table = table;
	}

	// 初始化设备列表
	public static JComboBox<String> setComboBox() {
		DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
		for (int i = 0; i < devices.length; i++) {
			model.addElement(devices[i].description);
		}
		jBox.setModel(model);
		jBox.setSelectedIndex(2);
		return jBox;
	}
}
